package pqt;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.List;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

public class AgregarBloque  {

	//private final JPanel panelAgregarBloque = new JPanel();
	private final JPanel panelCentral = new JPanel();
	private JTextField nombreBloque;
	private JTextField nombreAtributo;
	private JTextField nombreFalla;
	private List arbolAtributos = new List();
	private JList listaAtributos;
	private List arbolFallas = new List();
	private JList listaFallas;
	private BaseDeDatos db = BaseDeDatos.getInstance();
	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		try {
			AgregarBloque dialog = new AgregarBloque();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
*/
	/**
	 * Create the dialog.
	 */
	public AgregarBloque(JPanel panelAgregarBloque) {
		
		/*setBounds(0, 0, 1000, 1000);
		getContentPane().setLayout(new BorderLayout());
		panelAgregarBloque.setBackground(Color.WHITE);
		panelAgregarBloque.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(panelAgregarBloque, BorderLayout.CENTER);
		*/panelAgregarBloque.setLayout(null);
		
		panelCentral.setLayout(null);
		panelCentral.setBounds(150, 10, 974, 585);
		panelCentral.setBorder(new TitledBorder(new LineBorder(new Color(51, 153, 255)), "Agregar Bloque", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelAgregarBloque.add(panelCentral);
		
		nombreBloque = new JTextField();
		nombreBloque.setBounds(170, 33, 249, 26);
		panelCentral.add(nombreBloque);
		nombreBloque.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Nombre de Bloque:");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblNewLabel.setBounds(20, 30, 146, 26);
		panelCentral.add(lblNewLabel);
		
		JLabel lblAgregarAtributo = new JLabel("Agregar Atributo:");
		lblAgregarAtributo.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblAgregarAtributo.setBounds(20, 78, 119, 20);
		panelCentral.add(lblAgregarAtributo);
		
		nombreAtributo = new JTextField();
		nombreAtributo.setBounds(139, 77, 249, 27);
		panelCentral.add(nombreAtributo);
		nombreAtributo.setColumns(10);
		
		JLabel lblAgregarFalla = new JLabel("Agregar Falla:");
		lblAgregarFalla.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblAgregarFalla.setBounds(517, 78, 119, 20);
		panelCentral.add(lblAgregarFalla);
		
		nombreFalla = new JTextField();
		nombreFalla.setColumns(10);
		nombreFalla.setBounds(620, 77, 255, 27);
		panelCentral.add(nombreFalla);
		
		final JScrollPane scrollPaneAtributo;
			
		final JScrollPane scrollPaneFalla;
		
		listaAtributos = new JList(createLista(arbolAtributos));
		listaAtributos.setBackground(Color.WHITE);
		listaAtributos.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		scrollPaneAtributo = new JScrollPane(listaAtributos);
		scrollPaneAtributo.setBounds(20, 119, 419, 415);
		panelCentral.add(scrollPaneAtributo);
		
		
		listaAtributos.setCellRenderer(new CheckListRenderer());
		listaAtributos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listaAtributos.setBorder(null);
		listaAtributos.setFocusable(false);
		
		JButton btnAgregarAtributo = new JButton("+");
		btnAgregarAtributo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
						
				if(agregarItem(listaAtributos, arbolAtributos,nombreAtributo.getText()))
					nombreAtributo.setText("");
				
					
				
			}
		});
		btnAgregarAtributo.setBounds(398, 77, 41, 27);
		panelCentral.add(btnAgregarAtributo);
		
		
		
		listaFallas = new JList(createLista(arbolAtributos));
		listaFallas.setBackground(Color.WHITE);
		listaFallas.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		scrollPaneFalla = new JScrollPane(listaFallas);
		scrollPaneFalla.setBounds(517, 119, 419, 415);
		panelCentral.add(scrollPaneFalla);
		
		
		listaFallas.setCellRenderer(new CheckListRenderer());
		listaFallas.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listaFallas.setBorder(null);
		listaFallas.setFocusable(false);
		
		JButton btnAgregarFalla = new JButton("+");
		btnAgregarFalla.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(agregarItem(listaFallas, arbolFallas,nombreFalla.getText()))
					nombreFalla.setText("");
			}
		});
		btnAgregarFalla.setBounds(893, 76, 41, 26);
		panelCentral.add(btnAgregarFalla);
		
		JButton btnQuitarAtributos = new JButton("Quitar Marcados");
		btnQuitarAtributos.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				quitarItem(listaAtributos, arbolAtributos);
			}
		});
		btnQuitarAtributos.setBounds(20, 540, 136, 23);
		panelCentral.add(btnQuitarAtributos);
		
		JButton btnQuitarFallas = new JButton("Quitar Marcados");
		btnQuitarFallas.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				quitarItem(listaFallas, arbolFallas);
			}
		});
		btnQuitarFallas.setBounds(517, 540, 136, 23);
		panelCentral.add(btnQuitarFallas);
		
		JButton btnAceptar = new JButton("Aceptar");
		btnAceptar.setBounds(930, 605, 89, 23);
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					db.agregarBloque(nombreBloque.getText(), arbolAtributos,arbolFallas);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		panelAgregarBloque.add(btnAceptar);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.setBounds(1035, 605, 89, 23);
		panelAgregarBloque.add(btnCancelar);
		
		listaAtributos.addMouseListener(new MouseAdapter() {
			 public void mouseClicked(MouseEvent e) {
			   
				 if(arbolAtributos.getItemCount()>0){
					 int index = listaAtributos.locationToIndex(e.getPoint());
					 CheckableItem item = (CheckableItem) listaAtributos.getModel().getElementAt(index);
					 item.setSelected(!item.isSelected());
					 Rectangle rect = listaAtributos.getCellBounds(index, index);
					 listaAtributos.repaint(rect);
					 scrollPaneAtributo.validate();
					 scrollPaneAtributo.repaint();
				 }
			 }
			});
		
		listaFallas.addMouseListener(new MouseAdapter() {
			 public void mouseClicked(MouseEvent e) {
				 
				 if(arbolFallas.getItemCount()>0){
					 
					 int index = listaFallas.locationToIndex(e.getPoint());
					 CheckableItem item = (CheckableItem) listaFallas.getModel().getElementAt(index);
					 item.setSelected(!item.isSelected());
					 Rectangle rect = listaFallas.getCellBounds(index, index);
					 listaFallas.repaint(rect);
					 scrollPaneFalla.validate();
					 scrollPaneFalla.repaint();
				}
			 }
			});
		
	}
	
	private CheckableItem[] createLista(List strs) {
	    int n = strs.countItems();
	    CheckableItem[] items = new CheckableItem[n];
	    for (int i = 0; i < n; i++) 
	      items[i] = new CheckableItem(strs.getItem(i).toString());
	    
	    return items;
	  }
	
	private boolean agregarItem(JList lista, List arbol, String nombre) {
		
		if((!"".equals(nombre)) && (!existeItem(lista, arbol, nombre))){
			
			arbol.add(nombre);
			lista.setListData(createLista(arbol));
			lista.repaint();
			
			return true;
		}
		
		return false;
		
	}
	
	private void quitarItem(JList lista, List arbol) {
		ListModel model = lista.getModel();
		   int n = model.getSize();
		   for (int i = n-1; i >= 0; i--) {
		     CheckableItem item = (CheckableItem) model.getElementAt(i);
		     if (item.isSelected()) {
		    	arbol.delItem(i);
		    	
		     }
		   }
		   lista.setListData(createLista(arbol));
		   lista.repaint();
	}
	
	private boolean existeItem(JList lista, List arbol, String texto) {
		
		ListModel model = lista.getModel();
		int n = model.getSize();
		
		for (int i = n-1; i >= 0; i--) {
		     
			CheckableItem item = (CheckableItem) model.getElementAt(i);
		         	
			if(arbol.getItem(i).toString().equalsIgnoreCase(texto)) {
				JOptionPane.showMessageDialog(null,"El Item que desea agregar ya se encuentra en la lista.");
				return true;
			}
		}
		
		return false;
	}


}
